package com.hainingtong.user.shiro.realm;


import com.hainingtong.user.shiro.token.CustomUsernamePasswordToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.mindrot.jbcrypt.BCrypt;

@Slf4j
public class UsernamePasswordRealm extends BaseRealm {

    /**
     * 自定义匹配密码的方法
     */
    public UsernamePasswordRealm() {
        this.setCredentialsMatcher(new CredentialsMatcher() {
            @Override
            public boolean doCredentialsMatch(AuthenticationToken authenticationToken, AuthenticationInfo authenticationInfo) {
                return BCrypt.checkpw(authenticationToken.getCredentials().toString(), authenticationInfo.getCredentials().toString());
            }
        });
    }

    /**
     * 只处理对应类型的认证
     *
     * @param token
     * @return 如果是对应的类型就返回true，表示会执行doGetAuthorizationInfo获取用户信息用于认证；false就跳过当前realm
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof CustomUsernamePasswordToken;
    }

}
